//
//  MOWorkJiShiDetailOrAddViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/9/19.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOWorkJiShiDetailOrAddViewController.h"

@interface MOWorkJiShiDetailOrAddViewController ()<UITableViewDelegate,UITableViewDataSource,XLsn0wPickerTimerDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIView *footerView;

@property(nonatomic,strong)UIView *bottomView;
//自评内容
@property(nonatomic,strong)UITextView *contentTextView;
//展晒
@property(nonatomic,strong)NSString *zhanShaiStr;

@property(nonatomic,strong)NSString *noIdS;

@property(nonatomic,strong)NSMutableArray *buttonArray;

@end

@implementation MOWorkJiShiDetailOrAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self getData];
}

-(void)creatView
{
    self.title = @"工作纪实详情";
    
    self.view.backgroundColor = KGrayColor_f8f8f8;
    
    self.zhanShaiStr = @"1";
    
    [self.view addSubview:self.tableView];
    
    self.tableView.tableFooterView = self.footerView;
    
    [self.view addSubview:self.bottomView];
}

-(void)getData
{
    if ([self.idStr integerValue]==0) {
        return;
    }
    NSString *url = [NSString stringWithFormat:@"%@/%@?authorization=%@",SZRSGZJS_QUERYGZJSBYOID_URL,self.idStr,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{} success:^(id responseObject) {
        NSLog(@"工作纪实详情：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        self.dataDic = responseObject[@"resultData"];
        if (self.dataDic.allKeys.count>0) {
            if ([[NSString passNullForString:self.dataDic[@"zszt"]] integerValue]==0) {
                self.zhanShaiStr = @"0";
            }else{
                self.zhanShaiStr = @"1";
            }
            self.dateStr = [NSString passNullForString:self.dataDic[@"ksrq"]];
            [self.tableView reloadData];
        }else{
            self.zhanShaiStr = @"1";
        }
        
        if (self.dataDic.allKeys.count>0) {
            if ([[NSString passNullForString:self.dataDic[@"zszt"]] integerValue]==0) {
                [self.buttonArray[1] setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
                [self.buttonArray[0] setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
            }else{
                [self.buttonArray[0] setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
                [self.buttonArray[1] setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
            }
            
            if (self.dataDic.allKeys.count>0) {
                NSString *str = [NSString passNullForString:self.dataDic[@"gzjsnr"]];
                self.contentTextView.text = str.length>0?str:@"";
                if (str.length>0) {
                    self.contentTextView.placeholder = @"";
                }
            }
        }
        
    } failure:^(NSError *error) {
        NSLog(@"工作纪实详情：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

-(void)clickSubmitButton
{
    if (self.dateStr.length==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请选择时间" showInView:self.view];
        return;
    }
    if (self.contentTextView.text.length==0) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"请您输入自评内容" showInView:self.view];
        return;
    }
    
    MOHintView *hintView = [[MOHintView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) andContentStr:@"确定保存？"];
    __weak typeof (self)weakSelf = self;
    hintView.clickSureButtonBlock = ^{
        [weakSelf seaveData];
    };
    [WINDOW addSubview:hintView];
}

-(void)seaveData
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",SZRSGZJS_GZJSSAVE_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];

    self.noIdS = [NSString stringWithFormat:@"%@",@"none"];
    if (self.idStr.length>0) {
        self.noIdS = self.idStr;
    }
    NSDictionary *dic = @{
                          @"oid":self.noIdS,
                          @"rq":self.dateStr,
                          @"zszt":self.zhanShaiStr,
                          @"gzjsnr":self.contentTextView.text
                          };
    NSString *dicStr = [NSString dictionaryToJson:dic];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"data":dicStr} success:^(id responseObject) {
        NSLog(@"工作纪实保存：%@",responseObject);
        if ([responseObject[@"status"] integerValue]==1) {
            if (self.saveSuceessBlock) {
                self.saveSuceessBlock();
            }
        }
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:responseObject[@"msg"] showInView:self.view];
    } failure:^(NSError *error) {
        NSLog(@"工作纪实保存：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

-(void)clickGradeButton:(UIButton *)sender
{
    if (sender.tag==0) {
        self.zhanShaiStr = @"1";
    }else{
        self.zhanShaiStr = @"0";
    }
    for (int i = 0; i<self.buttonArray.count; i++) {
        UIButton *button = self.buttonArray[i];
        if (i==sender.tag) {
            [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
        }else{
            [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
        }
        [button SG_imagePositionStyle:(SGImagePositionStyleDefault) spacing:10];
    }
}

#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.textColor = KBlackColor_222222;
        cell.textLabel.font = FONT_SIZE(15);
        cell.detailTextLabel.font = FONT_SIZE(15);
        NSArray *arr = @[@"时间:"];
        cell.textLabel.text = arr[indexPath.row];
        
    }
    if ([self.dateStr integerValue]>0) {
        cell.detailTextLabel.text = self.dateStr;
    }else{
        cell.detailTextLabel.text = @"请选择";
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.8;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 13;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.idStr integerValue]>0) {
        return;
    }
//    XLsn0wPickerTimer *pickerDate = [[XLsn0wPickerTimer alloc] init];
//    pickerDate.maxComponentCount = 3;
//    pickerDate.xlsn0wDelegate = self;
//    [pickerDate show];
}

- (void)pickerTimer:(XLsn0wPickerTimer *)pickerTimer year:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {
    
    self.dateStr = [NSString stringWithFormat:@"%ld", (long)year];
    
    UITableViewCell *cell = (UITableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    cell.detailTextLabel.text = self.dateStr;
    
}


#pragma mark - 设置顶头分割线
-(void)viewDidLayoutSubviews {
    
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        if (@available(iOS 8.0, *)) {
            [self.tableView setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        if (@available(iOS 8.0, *)) {
            [cell setLayoutMargins:UIEdgeInsetsMake(0, 0, 0, 0)];
        } else {
            // Fallback on earlier versions
        }
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
}

#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 57 - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

-(UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(54) + 180)];
        _footerView.backgroundColor = [UIColor whiteColor];
        
        UILabel *labbel = [[UILabel alloc]initWithFrame:CGRectMake(15, 1, SCREEN_WIDTH, kHConversions(54))];
        labbel.text = @"是否展晒:";
        labbel.textColor = KBlackColor_222222;
        labbel.font = FONT_SIZE(15);
        [_footerView addSubview:labbel];
        
        self.buttonArray = [NSMutableArray array];
        CGFloat buttonW = 100;
        NSArray *arr = @[@"展晒",@"不展晒"];
        for (int i = 0; i<arr.count; i++) {
            UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 20 - buttonW*2 + buttonW*i, 0, buttonW, kHConversions(52))];
            [button setTitleColor:KBlackColor_222222 forState:UIControlStateNormal];
            button.titleLabel.font = FONT_SIZE(15);
            [button setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
            [button setTitle:arr[i] forState:UIControlStateNormal];
            [button SG_imagePositionStyle:(SGImagePositionStyleDefault) spacing:10];
            
            if (i==0) {
                [button setImage:IMAGE(@"work_selected") forState:UIControlStateNormal];
            }else{
                [button setImage:IMAGE(@"work_normal") forState:UIControlStateNormal];
            }
            
        
            button.tag = i;
            [button addTarget:self action:@selector(clickGradeButton:) forControlEvents:UIControlEventTouchUpInside];
            [_footerView addSubview:button];
            [self.buttonArray addObject:button];
        }
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, kHConversions(52)+1, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = KGrayColor_f8f8f8;
        [_footerView addSubview:lineView];
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(15, lineView.bottom+10, SCREEN_WIDTH - 30, kHConversions(35))];
        contentLabel.textColor = KBlackColor_222222;
        contentLabel.font = FONT_SIZE(15);
        contentLabel.text = @"自评内容:";
        [_footerView addSubview:contentLabel];
        
        self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(10, contentLabel.bottom, SCREEN_WIDTH - 20, 180)];
        self.contentTextView.textColor = KBlackColor_222222;
        self.contentTextView.font = FONT_SIZE(12);
        self.contentTextView.placeholder = @"请输入自评内容...";
        [_footerView addSubview:self.contentTextView];
        
        _footerView.height = self.contentTextView.bottom;
    }
    return _footerView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 57, SCREEN_WIDTH, 57)];
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height)];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:submitButton];
    }
    return _bottomView;
}


@end
